﻿using System;
using System.Collections.Generic;

namespace ArgusLight.Threading.SynchronizedObjects
{
	public class SynchronizedStack<T>
	{
		private object _lock;
		Stack<T> stack;

		public SynchronizedStack(Stack<T> Stack)
		{
			this.stack = Stack;
			this._lock = new object();
		}

		public SynchronizedStack()
			: this(new Stack<T>())
		{
			this._lock = new object();
		}

		public void Push(T item)
		{
			lock (this._lock)
			{
				this.stack.Push(item);
			}
		}

		public void PushRange(IEnumerable<T> items)
		{
			lock (this._lock)
			{
				IEnumerator<T> e = items.GetEnumerator();
				while (e.MoveNext() == true)
					this.stack.Push(e.Current);
			}
		}

		public T Peek()
		{
			lock (this._lock)
			{
				return this.stack.Peek();
			}
		}

		public T Pop()
		{
			lock (this._lock)
			{
				return this.stack.Pop();
			}
		}

		public bool TryPop(ref T value)
		{
			lock (this._lock)
			{
				if (this.stack.Count < 1)
					return false;
				value = this.stack.Pop();
				return true;
			}
		}

		public void Clear()
		{
			lock (this._lock)
			{
				this.stack.Clear();
			}
		}

		public int Count { get { return this.stack.Count; } }
	}
}
